package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.FileLibraryModel;
import com.ruoyi.system.domain.ShotModel;
import com.ruoyi.system.domain.dto.DeleteDto;
import com.ruoyi.system.domain.dto.QueryDto;
import com.ruoyi.system.domain.dto.ShotListDto;
import com.ruoyi.system.service.ShotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @Auther Yulong Liu
 * @Date 2023/12/18 11:53
 * @Description
 **/

@RestController
@RequestMapping("/shot_library")
public class ShotController {
    @Autowired
    private ShotService shotService;

    @PostMapping("/add")
    public R add(@RequestBody ShotModel shotModel){
        if (shotModel.getStToolsName() == null){
            return R.fail("必须提供工具的名称");
        }
        shotModel.setGmtCreated(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        shotModel.setGmtModified(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        shotModel.setCreator(SecurityUtils.getUserId());
        shotModel.setIsDeleted(0);
        boolean res = shotService.save(shotModel);
        if (res){
            return R.ok(null,"新增成功！");
        }else {
            return R.fail("保存失败！");
        }
    }

    @PostMapping("/list")
    public R list(@RequestBody ShotListDto shotListDto){
        /**@Description: 列举需要的工具的
         * @param [shotListDto]
         * @return R
         */
        if (shotListDto.getName() == null){
            return R.fail("必须提供想展示的Shot的名称");
        }

        LambdaQueryWrapper<ShotModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShotModel::getIsDeleted,0);
        queryWrapper.eq(ShotModel::getStToolsName,shotListDto.getName());
        List<ShotModel> shotModelList = shotService.list(queryWrapper);
        return R.ok(shotModelList);
    }


    @PostMapping("/update")
    public R update(@RequestBody ShotModel shotModel){
        if (shotModel.getId() == null){
            return R.fail("必须提供要修改的shot的id");
        }

        shotModel.setGmtModified(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        boolean res = shotService.updateById(shotModel);
        if (res){
            return R.ok(null,"修改成功！");
        }else {
            return R.fail("修改失败！");
        }
    }

    @PostMapping("/delete")
    public R delete(@RequestBody DeleteDto deleteDto){
        if (deleteDto == null){
            return R.fail("必须提供要删除的shot的id");
        }

        boolean res = shotService.removeById(deleteDto.getId());

        if (res){
            return R.ok(null,"删除成功！");
        }else {
            return R.fail("删除失败！");
        }
    }

    @PostMapping("/logic-delete")
    public R logicDelete(@RequestBody DeleteDto deleteDto){
        if (deleteDto.getId() == null){
            return R.fail("必须提供要删除的shot的id");
        }

        LambdaUpdateWrapper<ShotModel> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ShotModel::getIsDeleted,1).eq(ShotModel::getId,deleteDto.getId());
        boolean res = shotService.update(updateWrapper);

        if (res){
            return R.ok(null,"逻辑删除成功！");
        }else {
            return R.fail("逻辑删除失败！");
        }
    }
}
